package com.back.controller;

import com.back.common.enums.FeeStatus;
import com.back.common.utils.ExcelUtil;
import com.back.common.utils.ResultBean;
import com.back.dto.FeePageDTO;
import com.back.entity.FeeManagement;
import com.back.service.IFeeManagementService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 费用管理表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-01-02
 */
@RestController
@RequestMapping("/fee-management")
@RequiredArgsConstructor
public class FeeManagementController {

    private final IFeeManagementService feeManagementService;

    /**
     * 分页查询费用列表
     */
    @PostMapping("/page")
    public ResultBean<IPage<FeeManagement>> page(@RequestBody FeePageDTO dto) {
        IPage<FeeManagement> page = new Page<>(dto.getCurrent(), dto.getSize());
        LambdaQueryWrapper<FeeManagement> queryWrapper = new LambdaQueryWrapper<>();
        
        // 添加查询条件
        queryWrapper.eq(dto.getPayerType() != null, 
                       FeeManagement::getPayerType, 
                       dto.getPayerType());
        queryWrapper.eq(dto.getPayerId() != null, 
                       FeeManagement::getPayerId, 
                       dto.getPayerId());
        
        // 按创建时间倒序排序
        queryWrapper.orderByDesc(FeeManagement::getCreateTime);
        
        return ResultBean.ok(feeManagementService.page(page, queryWrapper));
    }

    /**
     * 新增费用项
     */
    @PostMapping("/add")
    public ResultBean<String> add(@RequestBody FeeManagement feeManagement) {
        feeManagement.setPaymentDate(LocalDate.now());
        feeManagement.setCreateTime(LocalDateTime.now());
        feeManagement.setUpdateTime(LocalDateTime.now());
        feeManagementService.save(feeManagement);
        return ResultBean.ok("添加成功");
    }

    /**
     * 确认费用项
     */
    @PostMapping("/confirm/{id}")
    public ResultBean<String> confirm(@PathVariable Long id) {
        FeeManagement fee = feeManagementService.getById(id);
        if (fee != null) {
            fee.setUpdateTime(LocalDateTime.now());
            fee.setStatus(FeeStatus.CONFIRMED.getCode());
            feeManagementService.updateById(fee);
            return ResultBean.ok("确认成功");
        }
        return ResultBean.fail("费用项不存在");
    }

    /**
     * 废弃费用项
     */
    @PostMapping("/discard/{id}")
    public ResultBean<String> discard(@PathVariable Long id) {
        feeManagementService.removeById(id);
        return ResultBean.ok("废弃成功");
    }

    /**
     * 导出已确认的费用项
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        try {
            // 查询已确认的费用项
            List<FeeManagement> feeList = feeManagementService.list();
            
            // 导出Excel
            ExcelUtil.export(
                response,
                feeList,
                "费用管理表",
                "费用列表",
                FeeManagement.class
            );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
